package com.sxfq.loansupermarket.backsystem.modular.marketing.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.marketing.dao.ReservationsAggregateDao;
import com.sxfq.loansupermarket.backsystem.modular.marketing.dao.ReturnVisitStatisticsDao;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReservationsAggregateInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReturnVisitStatistics;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.SearchReservationsAggregateInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.SearchReturnVisitStatistics;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.IReservationsAggregateService;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.ReturnVisitStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ReturnVisitStatisticsServiceImpl implements ReturnVisitStatisticsService {

    @Autowired
    private ReturnVisitStatisticsDao dao;

    @Autowired
    private SqlMapper sqlMapper;


    public String getTableName() {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "deal_info";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return tableName;
    }

    /**
     *查询回访统计数据
     *
     * @param page
     * @param searchInfo
     * @return
     */

    @Override
    public List<ReturnVisitStatistics> selectPageListByStatusMap(Page<ReturnVisitStatistics> page, SearchReturnVisitStatistics searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName())) {
            params.put("outpatientName", searchInfo.getOutpatientName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAccessDisease())) {
            params.put("accessDisease", searchInfo.getAccessDisease());
        }
        //建档时间
        if (StringUtil.isNotEmpty(searchInfo.getStartFilingTime())) {
            params.put("startFilingTime", searchInfo.getStartFilingTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndFilingTime())) {
            params.put("endFilingTime", searchInfo.getEndFilingTime());
        }
        //回访时间
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndRevisitTime())) {
            params.put("endRevisitTime", searchInfo.getEndRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisit())) {
            params.put("revisit", searchInfo.getRevisit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            String fieldTc = "";
            String fieldTcs[] = searchInfo.getFieldTc().split(",");
            if (fieldTcs.length == 1) {
                fieldTc = fieldTcs[0];
            } else {
                for (int i = 0; i < fieldTcs.length; i++) {
                    if (i == (fieldTcs.length - 1)) {
                        fieldTc += "'" + fieldTcs[i];
                    } else if (i == 0) {
                        fieldTc += fieldTcs[i] + "'" + ",";
                    } else {
                        fieldTc += "'" + fieldTcs[i] + "'" + ",";
                    }
                }
            }
            params.put("fieldTc",  "('"+fieldTc+"')");
        }
        params.put("page", page);
        params.put("tableName", this.getTableName());
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPages(page, params);
    }

    /**
     *查询回访统计总数
     *
     * @param page
     * @param searchInfo
     * @return
     */


    @Override
    public int getQueryPagesCount(Page<ReturnVisitStatistics> page, SearchReturnVisitStatistics searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName())) {
            params.put("outpatientName", searchInfo.getOutpatientName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getAccessDisease())) {
            params.put("accessDisease", searchInfo.getAccessDisease());
        }
        //建档时间
        if (StringUtil.isNotEmpty(searchInfo.getStartFilingTime())) {
            params.put("startFilingTime", searchInfo.getStartFilingTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndFilingTime())) {
            params.put("endFilingTime", searchInfo.getEndFilingTime());
        }
        //回访时间
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndRevisitTime())) {
            params.put("endRevisitTime", searchInfo.getEndRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisit())) {
            params.put("revisit", searchInfo.getRevisit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        return dao.getQueryPagesCount(page, params);
    }

    //获取总数
    @Override
    public int getCountByMap(Map<String, Object> params) {
        return dao.getDataCount(params);
    }
}
